-- drop all index

declare @cnt  int

select @cnt =0

DECLARE @tableName nvarchar (100)

DECLARE @indexName NVARCHAR(128)
DECLARE @dropIndexSql NVARCHAR(4000)



DECLARE tableNames CURSOR FOR
SELECT name FROM dbo.sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1
ORDER BY Name

OPEN tableNames
FETCH NEXT FROM tableNames INTO @tableName
WHILE @@fetch_status = 0
BEGIN
	--print @tableName
	DECLARE tableIndexes CURSOR FOR
	SELECT name FROM sysindexes
	WHERE id = OBJECT_ID(@tableName) AND 
	  indid > 0 AND indid < 255 AND
	  INDEXPROPERTY(id, name, 'IsStatistics') = 0
	  ORDER BY indid DESC
		
	
		OPEN tableIndexes
		FETCH NEXT FROM tableIndexes INTO @indexName
		WHILE @@fetch_status = 0
		BEGIN

		 select @cnt =@cnt+1

		 if left(@indexName,2)='PK'
		  BEGIN
			  print '--- PK ' + @indexName

		  SET @dropIndexSql = N'ALTER TABLE ' + @tableName +  ' drop constraint ' + @indexName
			 EXEC sp_executesql @dropIndexSql

		  END
		 else 	
		   BEGIN		

			   print '--- IX ' + @indexName

			  SET @dropIndexSql = N'DROP INDEX ' + @tableName +  '.' + @indexName
			  EXEC sp_executesql @dropIndexSql

		   END	

		  FETCH NEXT FROM tableIndexes INTO @indexName
		END
		
		
		CLOSE tableIndexes
		DEALLOCATE tableIndexes
	

  FETCH NEXT FROM tableNames INTO @tableName
END

CLOSE tableNames
DEALLOCATE tableNames


 print 'Total deleted : ' + convert(nvarchar(3), @cnt)
